#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using System;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Neatly.UI
{
    public class NDragListener : NUIBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
    {
        private bool IsDrag {get; set;}
        private Action<float, float> m_DragStartAction;
        private Action<float, float> m_DragAction;
        private Action<float, float> m_DragEndAction;

        public void OnBeginDrag(PointerEventData eventData)
        {
            if (Input.touchCount <= 1)
            {
                IsDrag = true;
                var pos = eventData.position;
                m_DragStartAction?.Invoke(pos.x - NeatlyConfig.CANVAS_WIDTH / 2, pos.y);
            }
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (IsDrag && Input.touchCount <= 1)
            {
                //记录当前位置
                var pos = eventData.position;
                m_DragAction?.Invoke(pos.x - NeatlyConfig.CANVAS_WIDTH / 2, pos.y);
            }
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            IsDrag = false;
            var pos = eventData.position;
            m_DragEndAction?.Invoke(pos.x - NeatlyConfig.CANVAS_WIDTH / 2, pos.y);
        }

        public void AddDragAction(Action<float, float> onDragStartAction, Action<float, float> onDragAction, Action<float, float> onDragEndAction)
        {
            m_DragStartAction = onDragStartAction;
            m_DragAction = onDragAction;
            m_DragEndAction = onDragEndAction;
        }
    }
}